<?php

require_once 'UsersModel.php';
require_once 'UsersView.php';

/**
 * Controls the flow of the User UI. 
 */
class UsersController
{
	/**
	* Starts to controlling the flow.
	*
	* @return string The html flow.
	*/
	public function DoControll()
	{
		$html = '';

		$userModel = new UsersModel();
		$userView = new UsersView();

        // Delete user if the administrator chose to.
		if ( $userView->DeleteUser() )
		{
			if ( $userModel->DeleteUser( $userView->GetListUserID() ) )
			{
                MessageHandler::AddMessage( "User <em>{$userView->GetListUserName()}</em> has been deleted." );
			}
            else
            {
                MessageHandler::AddError( "Could not delete user <em>{$userView->GetListUserName()}</em>." );
            }
		}

		// Save the user if the administrator chose to.
		if ( $userView->EditSave() )
		{
            if ( ! preg_match( '/^[a-zA-Z0-9_]{3,20}$/i', $userView->GetEditUsername() ) )
            {
                MessageHandler::AddError( "Invalid username. Must have a length of 3-20 characters (a-z 0-9 underscore or dash)." );
            }
            
            if ( ! preg_match( '/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $userView->GetEditEmail() ) )
            {
                MessageHandler::AddError( "Invalid email address." );
            }
        
            if ( MessageHandler::CountErrors() === 0 )
            {
                if ( $userModel->SaveChanges( $userView->GetEditUserID(), $userView->GetEditUsername(), $userView->GetEditNewPassword(), $userView->GetEditEmail() ) )
                {
                    MessageHandler::AddMessage( "The information for user <em>{$userView->GetEditUsername()}</em> has been updated." );
                }
                else
                {
                    MessageHandler::AddError( "Something went wrong, could not save changes for user <em>{$userView->GetEditUsername()}</em>." );
                }
            }
		}
		
		// Try to create the user if the administrator chose to.
		if ( $userView->CreateSave() )
		{
			if ( ! preg_match( '/^[a-zA-Z0-9_]{3,20}$/i', $userView->GetCreateUsername() ) )
            {
                MessageHandler::AddError( "Invalid username. Must have a length of 3-20 characters (a-z 0-9 underscore or dash)." );
            }
			else if ( ! preg_match( '/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $userView->GetCreateEmail() ) )
            {
                MessageHandler::AddError( "Invalid email address." );
            }
			else
			{
				if ( $userModel->CreateUser( $userView->GetCreateUsername(), $userView->GetCreatePassword(),
			        $userView->GetCreateEmail() ) )
			    {
				    MessageHandler::AddMessage( "User has been created." );
			    }
			    else
			    {
				    MessageHandler::AddError( "Something went wrong, could not create the user." );
			    }
			}
		}
        
        $html .= MessageHandler::PrintMessages();
        
        // Add User Edit Form if the administrator chose to edit a user.
		if ( $userView->EditUser() )
		{
			$html .= $userView->DoEditUserForm( $userModel->GetUserByID( $userView->GetListUserID() ), $userModel->GetAllPermissions(), $userModel->GetPermissionsByUserID( $userView->GetListUserID() ) );
		}
		
		// Show register user form if the administrator chose to.
		if ( $userView->CreateUser() )
		{
			$html .= $userView->DoRegisterForm( $userModel->GetAllPermissions() );
		}

		// Add a table listing all Users.
		$html .= $userView->DoUserList( $userModel->GetAllUsers(), $userModel->GetAllPermissions() );
		
		$html .= $userView->CreateUserButton();
		
		$html .= "<br />----------------------------------------------------------<br />";
		$html .= "test - ". sha1("test") . "<br />";
		$html .= "----------------------------------------------------------<br />";
		
		return $html;
	}
}
